package com.graduate.cec.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.CommentMapper;
import com.graduate.cec.mapper.UserMapper;
import com.graduate.cec.pojo.dto.CommentSimpleDTO;
import com.graduate.cec.pojo.dto.UserSimpleDTO;
import com.graduate.cec.pojo.entity.Comment;
import com.graduate.cec.pojo.entity.CommentExample;
import com.graduate.cec.pojo.entity.User;
import com.graduate.cec.pojo.entity.UserExample;
import com.graduate.cec.pojo.vo.CommentSimpleVO;
import com.graduate.cec.pojo.vo.CommentVO;
import com.graduate.cec.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    /****************        增       ******************************************************************/
    public void insertComment(Comment comment){
        log.info("返回的comment:{}",comment);
        int i = commentMapper.insertSelective(comment);

        if (i != 1){
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }












    /****************        删       ******************************************************************/
    public void deleteByKey(Long id){
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andIdEqualTo(id);
        int i = commentMapper.deleteByExample(commentExample);
        if (i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }

    public void deleteByKeys(List<Long> id){
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();


        criteria.andIdIn(id);
        int i = commentMapper.deleteByExample(commentExample);
        if (i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }







    /****************        改       ******************************************************************/
    public void updateByKey(Comment comment){
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andIdEqualTo(comment.getId());
        int i = commentMapper.updateByExampleSelective(comment,commentExample);
        if (i == 1){
            return;
        }else{
            throw new CecServiceException(ResponseCode.UPDATE_INFO_ERROR,"修改失败");
        }
    }

    /****************        查       ******************************************************************/
    public List<Comment> getAllList(){
        CommentExample commentExample = new CommentExample();
        List<Comment> categories = commentMapper.selectByExample(commentExample);
        return categories;
    }

    public List<CommentVO> getCommentById(Long id){
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andArticleIdEqualTo(id);

        List<Comment> comments = commentMapper.selectByExampleWithBLOBs(commentExample);

        List<CommentVO> commentVOS = CopyUtil.copyList(comments, CommentVO.class);

        for (CommentVO commentVO : commentVOS) {
            User user = userMapper.selectNameById(commentVO.getUserId());

            if(commentVO.getBeenId() != null){
                Comment comment = commentMapper.selectByPrimaryKey(commentVO.getBeenId());
                User user1 = userMapper.selectNameById(comment.getUserId());
                if(user1 != null){
                    commentVO.setBeenName(user1.getNickname());
                }
            }
            commentVO.setUsername(user.getNickname());
            commentVO.setAvatar(user.getAvatar());
        }
        System.out.println(commentVOS);
        return commentVOS;
    }

    public JsonPage<Comment> getListByExample(CommentSimpleDTO commentSimpleDTO){

        PageHelper.startPage(commentSimpleDTO.getPageNum(), commentSimpleDTO.getPageSize());

        CommentExample commentExample = new CommentExample();
//        if(!("".equals(commentSimpleDTO.getNickname())) && commentSimpleDTO.getNickname() != null){
//            CommentExample.Criteria criteria = commentExample.createCriteria();
//            criteria.andNicknameEqualTo(commentSimpleDTO.getNickname());
//        }

        List<Comment> comments = commentMapper.selectByExampleWithBLOBs(commentExample);

        return JsonPage.restPage(new PageInfo<>(comments));
    }

    public JsonPage<CommentSimpleVO> getList(CommentSimpleDTO commentSimpleDTO){

        PageHelper.startPage(commentSimpleDTO.getPageNum(), commentSimpleDTO.getPageSize());

        List<CommentSimpleVO> list = commentMapper.getList(commentSimpleDTO);

        return JsonPage.restPage(new PageInfo<>(list));
    }

}